package com.zazu.mycl.application.commands;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.zazu.mycl.application.CommandRunner;
import com.zazu.mycl.application.terminalCommand;

public class PortListenCommand implements terminalCommand {

	public String getSignature() {
		// TODO Auto-generated method stub
		return "[Port] [IP]";
	}

	public boolean isThreadable() {
		// TODO Auto-generated method stub
		return true;
	}

	public void run(String[] args, CommandRunner commandRunner,
			String commandLine, String outputFileName) {

		try
		{
			if(args.length == 2)
			{
				
                                //An args length of 2 means only a port is specified
                                int portAdress = Integer.parseInt(args[1]);
				ServerSocket mySocket = new ServerSocket(portAdress);
				commandRunner.SetLine("Opening port.");
                                mySocket.setSoTimeout(40000);
				commandRunner.SetLine("Listening on port "+args[1]+".");
				
				while(true)
				{
					commandRunner.SetLine("New socket opened.");
					
					Socket s = mySocket.accept();
					
					
					InputStream is = s.getInputStream();
					OutputStream os = s.getOutputStream();
					
					
					PrintStream out = new PrintStream(os);
					Scanner in = new Scanner(is);
					
					out.println("Welcome from virtual server (MyCl).");
					
					while(in.hasNextLine())
					{
						commandRunner.SetLine(in.nextLine());
						out.flush();
					}
				}
				
			}
			else if(args.length == 3)
			{
				//TODO implement listening to another computers port
				//An args length of 3 means a port and an IP is specified
				//ServerSocket mySocket = new ServerSocket(args[1]);
                                commandRunner.SetLine("Port listen for other computers is not implemented yet.");
			
                                //An args length of 2 means only a port is specified
                                int portAdress = Integer.parseInt(args[1]);
                                
                                try {
                                    InetAddress tIN = InetAddress.getByName(args[2]);
                                    
                                    commandRunner.SetLine("Opening ip " + args[2] + " on port " + args[1] + ".");
                                
                                ServerSocket mySocket = new ServerSocket(portAdress, 0, tIN);
				mySocket.setSoTimeout(40000);
				commandRunner.SetLine("Listening on port "+args[1]+".");
				
				while(true)
				{
					commandRunner.SetLine("New socket opened.");
					
					Socket s = mySocket.accept();
					
					
					InputStream is = s.getInputStream();
					OutputStream os = s.getOutputStream();
					
					
					PrintStream out = new PrintStream(os);
					Scanner in = new Scanner(is);
					
					out.println("Welcome from virtual server (MyCl).");
					
					while(in.hasNextLine())
					{
						commandRunner.SetLine(in.nextLine());
						out.flush();
					}
				}
                                    
                             
                       } catch (UnknownHostException e) {
                        commandRunner.SetLine("Cannot resolve address: "+args[2]+".");
                    }
                                
                                
				
                        
                        }
			else
			{
				commandRunner.SetLine("Wrong arguments specified.");
			}
		}
		catch(IOException e)
		{
			commandRunner.SetLine("Could not listen to port " + args[1]);
		}
	
	}

	public void Listen(ServerSocket s)
	{
		
	}
	
	public void runSubCommand(String[] args, String commandLine) {
		// TODO Auto-generated method stub

	}

}
